﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OnlineBookStoreModel;

/// <summary>
/// Description résumée de DataAccess
/// </summary>
public class DataAccess
{
	public DataAccess()
	{
		
	}

    public static IList<Category> getCategories()
    {
        using (var db = new OnlineBookStoreEntities4())
        {
            return db.Categories.OrderBy(x => x.Name).ToList();
        }
    }

    public static IList<SubCategory> getSubCategories(String id)
    {
        using (var db = new OnlineBookStoreEntities4())
        {
            return db.SubCategories.Where(x => x.CategoryID == id).OrderBy(x => x.Name).ToList();
        }
    }

    public static void sendFeedback(String subject, String content, String firstname, String lastname, String email)
    {
        using (var db = new OnlineBookStoreEntities4())
        {
            FeedBack temp = new FeedBack() { Subject = subject,
                                             Content = content,
                                             DateCreated = DateTime.Today,
                                             FirstName = firstname,
                                             LastName = lastname,
                                             Email = email,
                                             Status = false};
            db.AddToFeedBacks(temp);
            db.SaveChanges();
        }
    }

    public static IList<Product> getAllProducts()
    {
        using (var db = new OnlineBookStoreEntities4())
        {
            return db.Products.OrderBy(x => x.ID).ToList();
            
        }
    }

    public static IList<Product> getCateProducts(String id)
    {
        using (var db = new OnlineBookStoreEntities4())
        {
            int idz = db.SubCategories.Where(x => x.CategoryID == id).OrderBy(x => x.CategoryID).FirstOrDefault().ID;
            return db.Products.Where(x => x.SubCategoryID == idz).OrderBy(x => x.ID).ToList();
            
        }
    }

    public static IList<Product> getSubProducts(String id)
    {
        using (var db = new OnlineBookStoreEntities4())
        {
            int idz = int.Parse(id);
            return db.Products.Where(x => x.SubCategoryID == idz).OrderBy(x => x.ID).ToList();

        }
    }

    public static IList<Product> getDetailsProducts(String id)
    {
        using (var db = new OnlineBookStoreEntities4())
        {
            return db.Products.Where(x => x.ID == id).ToList();

        }
    }

    public static bool getAdmin(String name, String pass)
    {
        using (var db = new OnlineBookStoreEntities4())
        {
            var temp = db.AdminLogins.Where(x => x.Username == name).First();
            if (pass.Equals(temp.Password))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    public static IList<FeedBack> getFeedbackList()
    {
        using (var db = new OnlineBookStoreEntities4())
        {
            return db.FeedBacks.OrderBy(x => x.Subject).ToList();

        }
    }

    public static FeedBack getFeedbackContent(String id)
    {
        using (var db = new OnlineBookStoreEntities4())
        {
            if (id != null)
            {
                int idz = int.Parse(id);
                return db.FeedBacks.Where(x => x.ID == idz).First();
            }
            else
            {
                return null;
            }

        }
    }

    public static void deleteFeedback(String id)
    {
        using (var db = new OnlineBookStoreEntities4())
        {
            int idz = int.Parse(id);
            FeedBack temp = db.FeedBacks.Where(x => x.ID == idz).First();

            db.FeedBacks.DeleteObject(temp);
            db.SaveChanges();

        }
    }
}